/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HAPSIEGestionDeAreas.java
 *
 * Created on 23/01/2012, 07:41:32 PM
 */
package Interfaz;


import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author erika
 */
public class GestionDeAreas extends javax.swing.JDialog {
 // Area[] area = new Area[10];
    ArrayList<Area> areas =new ArrayList();
    int filas=0;  
    /** Creates new form HAPSIEGestionDeAreas */
    public GestionDeAreas(java.awt.Dialog parent) {
        super(parent);
        initComponents();
        aviso.setText("");
    }
 /*   public void meteAreasLista(){
        areas.add(new Area("salud", "erika"));
        areas.add(new Area ("seguridad", "juancho"));
        areas.add(new Area("servicios al cliente", "ariadna"));
        areas.add (new Area("relaciones internacionales", "ana" ));     
    }
*/
   public ArrayList<Area> getAreas() {
        return areas;
    }

    public void setAreas(ArrayList<Area> areas) {
        this.areas = areas;
    }
    //Código Para Pruebas
   /**
     String  [][]  elems = {{"salud","erika"},{"seguridad", "juancho"}, 
              {"servicios", "ariadna"},{"relaciones", "ana"},{"lalalal", "miguel"}};
     String [] nombresColum = {"Nombre del Área", "Encargado"};
     
    public void meteAreas(){   
     
    area[0] = new Area("saud", "erika");
    area[1] = new Area("saud", "erika");
    area[2] = new Area("saud", "erika");
    area[3] = new Area("saud", "erika");
    }
 */   
  /** Recibe una lista de vectores los cuales irá posicionando en la 
     tabla*/
   /** public void actualizaTabla(){
        meteAreasActuales();
    DefaultTableModel modelo = (DefaultTableModel)tablaDeAreas.getModel();
    for(int i = 0; i < areas.size(); i++){
        modelo.setValueAt(areas.get(i).getNombre() , i, 0 );
    }
   for(int j = 0; j<areas.size(); j++){
       modelo.setValueAt(areas.get(j).getEncargado(), j, 1);
       
   } 

    }*/

    public String imprimeLista (){
      String imprime = "";
      String title = "La lista actualizada en la base de datos es:";
        for(int i = 0; i< areas.size(); i++){
          imprime = imprime + "Area :" + areas.get(i).getNombre() + "  Encargado :" + areas.get(i).getEncargado()+ "\n";
        }
        return title + imprime;
    }   
   // public void agregaTodasLasFilas(){
     //   DefaultTableModel modelo = (DefaultTableModel) tablaDeAreas.getModel();
       //  for(int i = 0; i<area.length; i++){
        //     modelo.addRow(elems);
             // 2 modelo.setValueAt(area[i].getNombre(), i, 0);
             //tablaDeAreas.setValueAt(area[i].getNombre(), filas, 0);
   // }
      //  for(int i = 0; i<area.length; i++){
        //   modelo.addRow(elems);
            // 2 modelo.setValueAt(area[i].getEncargado(), i, 1);
            //   tablaDeAreas.setValueAt(area[i].getEncargado(), filas, 1);
    //}         
    //}  
    
    
////////////////////////////////////////////////////////////////////////
    private void agregarFila(String nombre, String encargado){ 
        DefaultTableModel modelo = (DefaultTableModel) tablaDeAreas.getModel();    
        modelo.addRow(new Object[filas]);  
        tablaDeAreas.setValueAt(nombre,filas , 0);  
        tablaDeAreas.setValueAt(encargado, filas, 1);  
        filas = filas+1;  
 } 
 
   private void agregarFilaPorIndice(String nombre, String encargado, int indice){ 
        DefaultTableModel modelo = (DefaultTableModel) tablaDeAreas.getModel();    
        modelo.setValueAt(nombre, indice, 0);
        modelo.setValueAt(encargado, indice, 1);
 }   
    
////////////////////////////////////////////////////////////////////////
 private void borraFila(int index){   
      DefaultTableModel modelo=(DefaultTableModel) tablaDeAreas.getModel();  
      if(filas == 0){
          aviso.setText("Ya no existen elementos por borrar");
      }else{
          modelo.removeRow(index);
      }
    }
////////////////////////////////////////////////////////////////////////       
 public void iniciaTabla(){
    // tablaDeAreas = new JTable(elems, nombresColum);
 }   
 //////////
 
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaDeAreas = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        textoArea = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        textoEncargado = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        botonRegresaMenuGestionAreas = new javax.swing.JButton();
        botonBorrarMini = new javax.swing.JButton();
        botonEditaMini = new javax.swing.JButton();
        botonAgregarMini = new javax.swing.JButton();
        aviso = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("gestionDeAreas"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(UsoHAPSIE.HAPSIEApp.class).getContext().getResourceMap(GestionDeAreas.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tablaDeAreas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Nombre del Área:", "Encargado *"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tablaDeAreas.setName("tablaDeAreas"); // NOI18N
        jScrollPane1.setViewportView(tablaDeAreas);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        textoArea.setName("textoArea"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        textoEncargado.setName("textoEncargado"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        botonRegresaMenuGestionAreas.setText(resourceMap.getString("botonRegresaMenuGestionAreas.text")); // NOI18N
        botonRegresaMenuGestionAreas.setName("botonRegresaMenuGestionAreas"); // NOI18N
        botonRegresaMenuGestionAreas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonRegresaMenuGestionAreasActionPerformed(evt);
            }
        });

        botonBorrarMini.setText(resourceMap.getString("botonBorrarMini.text")); // NOI18N
        botonBorrarMini.setName("botonBorrarMini"); // NOI18N
        botonBorrarMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarMiniActionPerformed(evt);
            }
        });

        botonEditaMini.setText(resourceMap.getString("botonEditaMini.text")); // NOI18N
        botonEditaMini.setName("botonEditaMini"); // NOI18N
        botonEditaMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEditaMiniActionPerformed(evt);
            }
        });

        botonAgregarMini.setText(resourceMap.getString("botonAgregarMini.text")); // NOI18N
        botonAgregarMini.setName("botonAgregarMini"); // NOI18N
        botonAgregarMini.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarMiniActionPerformed(evt);
            }
        });

        aviso.setText(resourceMap.getString("aviso.text")); // NOI18N
        aviso.setName("aviso"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(49, 49, 49)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                        .addContainerGap(505, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonAgregarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(43, 43, 43)
                                .addComponent(botonEditaMini, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(39, 39, 39)
                                .addComponent(botonBorrarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 222, Short.MAX_VALUE)
                                .addComponent(botonRegresaMenuGestionAreas)))
                        .addGap(112, 112, 112))))
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addComponent(aviso, javax.swing.GroupLayout.PREFERRED_SIZE, 376, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(472, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(557, 557, 557)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addComponent(textoEncargado, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                        .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(textoArea, javax.swing.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE))
                    .addGap(131, 131, 131)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(aviso)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 49, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonAgregarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonEditaMini, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonBorrarMini, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(botonRegresaMenuGestionAreas, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addComponent(jLabel2)
                .addGap(25, 25, 25))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(78, 78, 78)
                    .addComponent(jLabel4)
                    .addGap(18, 18, 18)
                    .addComponent(textoArea, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(33, 33, 33)
                    .addComponent(jLabel5)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(textoEncargado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(285, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
// TODO add your handling code here:
     setVisible(false);
    getOwner().setVisible(true);
}//GEN-LAST:event_formWindowClosing

private void botonRegresaMenuGestionAreasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonRegresaMenuGestionAreasActionPerformed
// TODO add your handling code here:
    setVisible(false);
    getOwner().setVisible(true);
}//GEN-LAST:event_botonRegresaMenuGestionAreasActionPerformed

private void botonBorrarMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarMiniActionPerformed
  int indice = -1;
  indice = tablaDeAreas.getSelectedRow();
  if(indice == -1){
      JOptionPane.showMessageDialog(this, "Seleccione el Área a eliminar");
  }  else{
   
  borraFila(indice);
     
  }
  tablaDeAreas.clearSelection();
  
  
  System.out.println(imprimeLista());
   /** if(fila != -1 ){
       int response = JOptionPane.showConfirmDialog(this, "¿Seguro que desea eliminar ésta Área?");
         if(response == JOptionPane.YES_NO_OPTION ){         
         
       
         modeloTabla.removeRow(fila);
         tablaDeAreas.setModel(modeloTabla);
             //Borramos el registro
         }
         if(response == JOptionPane.NO_OPTION){
         }
         
     }else{
         JOptionPane.showMessageDialog(this, "Por favor seleccione el área a eliminar");    
     }
 */
}//GEN-LAST:event_botonBorrarMiniActionPerformed

private void botonEditaMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEditaMiniActionPerformed
 // tablaDeAreas.setModel(modeloTabla);
    String ar,en = "";
    int indice = -1;
     indice = tablaDeAreas.getSelectedRow();
   if(indice == -1){
       JOptionPane.showMessageDialog(this, "Seleccionar el Área a editar");
   }else{
      
      ar = (String) tablaDeAreas.getValueAt(indice, 0);
      en = (String) tablaDeAreas.getValueAt(indice, 1);
      //Se muestra el área para que se realicen los cambios
      textoArea.setText(ar);
      textoEncargado.setText(en);
     
    }
    // tablaDeAreas.clearSelection();
             //setVisible(false);
  // editaAreas.setLocationRelativeTo(null);
  // editaAreas.setVisible(true);
    System.out.println(imprimeLista());
}//GEN-LAST:event_botonEditaMiniActionPerformed

private void botonAgregarMiniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarMiniActionPerformed
 aviso.setText("");  
 //System.out.println("el texto del area fue"+textoArea.getText());
  int indice = -1;
  indice = tablaDeAreas.getSelectedRow();
 
   //agregaTodasLasFilas();   
    if(textoArea.getText().equalsIgnoreCase("") ){
  //System.out.println("el area es nula");
        JOptionPane.showMessageDialog(this, "Introduzca el nombre del Área");
  }else{
        if(indice == -1){
        if(textoEncargado.getText().equalsIgnoreCase("")){
            
     agregarFila(textoArea.getText(),"");
     Area nueva = new Area(textoArea.getText(),"");
     areas.add(nueva);
     
        }else{
     agregarFila(textoArea.getText(), textoEncargado.getText());
     Area nueva = new Area(textoArea.getText(), textoEncargado.getText());
     areas.add(nueva);
        }
        }else{
         if(textoArea.getText() == ""){
             JOptionPane.showMessageDialog(this, "Introduzca el elemento a agregar");
             
         }
         Area ar = new Area(textoArea.getText(), textoEncargado.getText());
         areas.set(indice, ar);
         tablaDeAreas.setValueAt(textoArea.getText(), indice, 0);
         tablaDeAreas.setValueAt(textoEncargado.getText(), indice, 1)        ;
    }
    }
    textoArea.setText("");
    textoEncargado.setText("");
   tablaDeAreas.clearSelection();
   
   System.out.println(imprimeLista());
   /**  
    meteAreasArr();  
  meteAreasLista();
  //  actualizaTabla();
    DefaultTableModel modelo = (DefaultTableModel)tablaDeAreas.getModel();
    int cont =0;
    while( cont < areas.size()){
    modelo.setValueAt(areas.get(cont).getNombre(), cont, 0  );
    cont++;
    }
*/
    
    /**setVisible(false);
    agregaAreas.setLocationRelativeTo(null);
   agregaAreas.setVisible(true); */    
}//GEN-LAST:event_botonAgregarMiniActionPerformed


    /**
     * @param args the command line arguments
     */
        

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel aviso;
    private javax.swing.JButton botonAgregarMini;
    private javax.swing.JButton botonBorrarMini;
    private javax.swing.JButton botonEditaMini;
    private javax.swing.JButton botonRegresaMenuGestionAreas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaDeAreas;
    private javax.swing.JTextField textoArea;
    private javax.swing.JTextField textoEncargado;
    // End of variables declaration//GEN-END:variables

private JDialog  agregaAreas = new AgregaArea(this); 
private JDialog  editaAreas = new EditaArea(this);
DefaultTableModel modeloTabla = new DefaultTableModel();
int fila = 1;
//int columna = -1;

//Lista de las Areas actuales
      
}
